﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace FileDuplicateUtility
{
    public class DuplicateSet : DependencyObject
    {
        public DuplicateSet(File a, File b)
        {
            Files = new ObservableCollection<DuplicateFile>();
            AddFile(a);
            AddFile(b);
        }

        public ObservableCollection<DuplicateFile> Files { get; set; }
        public IEnumerable<File> CheckedFiles { get { return Files.Where(x => x.IsChecked).Select(x => x.File); } }

        public string Name { get { return Files[0].File.Name; } }
        public long Size { get { return Files[0].File.Size; } }

        public int CheckedCount
        {
            get { return (int)GetValue(CheckedCountProperty); }
            set { SetValue(CheckedCountProperty, value); }
        }

        public static readonly DependencyProperty CheckedCountProperty =
            DependencyProperty.Register("CheckedCount", typeof(int), typeof(DuplicateSet), new UIPropertyMetadata(0));

        public void AddFile(File file)
        {
            Files.Add(new DuplicateFile(this, file));
        }

        internal void UpdateCheckedCount()
        {
            CheckedCount = Files.Count(x => x.IsChecked);
        }
    }

    public class DuplicateFile : DependencyObject
    {
        public DuplicateFile(DuplicateSet set, File file)
        {
            Set = set;
            File = file;
        }

        public DuplicateSet Set { get; private set; }
        public File File { get; private set; }

        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool), typeof(DuplicateFile), new UIPropertyMetadata(false, IsChecked_PropertyChanged));

        private static void IsChecked_PropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var obj = (sender as DuplicateFile);
            obj.Set.UpdateCheckedCount();
        }
    }
}
